package cn.framelife.rainbow.db.creator.part;

import java.util.Set;

import cn.framelife.rainbow.db.entity.ClazzProperty;
import cn.framelife.rainbow.db.entity.ImportSet;
import cn.framelife.rainbow.db.entity.Property;
import cn.framelife.rainbow.util.FreemarkerUtil;
import cn.framelife.rainbow.util.StringUtil;

public class ActionCreator {
	private String projectName;
	private String packageURL;
	
	public ActionCreator(String projectName, String packageURL) {
		this.projectName = projectName;
		this.packageURL = packageURL;
	}
	
	public void createCodeForAction(ClazzProperty entity) throws Exception{
		FreemarkerUtil.createAction(createAction(entity));
	}
	
	/**
	 * 生成实体类相关的Action对象
	 *
	 * @param reverseProperty
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	private ClazzProperty createAction(ClazzProperty entity) throws Exception {
		Set<String> imports = new ImportSet<String>();
		ClazzProperty action = new ClazzProperty();
		action.setClassName(new StringBuffer(entity.getClassName()).append("Action").toString());
		action.setProjectName(projectName);
		
		action.setPackageURL(StringUtil.merge(packageURL,".struts"));
		
		imports.add(entity.getPkType().getJavaTypePackage());
		imports.add(StringUtil.merge(entity.getPackageURL(),".",entity.getClassName()));
		imports.add(StringUtil.merge(packageURL,".service.I",entity.getClassName(),"Service"));
		action.setImports(imports);
		
		action.setEntityName(entity.getClassName());
		Property property = new Property();
		property.setType(StringUtil.merge("I",entity.getClassName(),"Service"));
		property.setName(StringUtil.merge(StringUtil.toLowerCaseFirstOne(entity.getClassName()),"Service"));
		action.setProperty(property);
		
		return action;
	}

}
